const { Log, File } = require("./buildCore");
const { vueTemplate } = require("./template/viewTemplateTs");
const log = new Log();
const file = new File();

log.info("~~~~~~开始创建pages文件夹下模板~~~~~~~~~");
log.info(
  "请输入要生成的页面组件名称、会生成在 pages/目录下(请使用大驼峰命名规范 例：BaseModal)"
);
let componentName = "";
process.stdin.on("data", async chunk => {
  const inputName = String(chunk)
    .trim()
    .toString();

  // 驼峰转换
  const lower = file.toLower(inputName);
  // vue组件的目录
  let componentVueName = file.resolve("../../src/pages", inputName);
  if (!componentVueName.endsWith(".vue")) {
    componentVueName += ".vue";
  }
  const componentDirectory = file.dirname(componentVueName);
  const hasComponentExists = file.exist(componentVueName);
  if (hasComponentExists) {
    log.error(`${inputName}页面组件已存在，请重新输入`);
    return;
  } else {
    log.info(`正在生成 component 目录 ${componentDirectory}`);
    await file.mkdir(componentDirectory);
  }

  // vue组件的后缀.vue
  try {
    if (inputName.includes("/")) {
      const inputArr = inputName.split("/");
      componentName = inputArr[inputArr.length - 1];
    } else {
      componentName = inputName;
    }
    log.info(`正在生成 vue 文件 ${componentVueName}`);
    await file.creat(componentVueName, vueTemplate(componentName, lower));
    log.success("生成成功");
  } catch (e) {
    log.error(e.message);
  }

  process.stdin.emit("end");
});

process.stdin.on("end", () => {
  process.exit();
  log.info("exit");
});
